<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-02-25
 * Time: 16:39
 */
class Topic extends MY_Controller{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 分类列表
     */
    public function category_list(){
        $pageTitle = "分类列表";
        $page = $this->input->get_post('page') ? $this->input->get_post('page') : 1;
        $pagesize = $this->input->get_post('pagesize') ? $this->input->get_post('pagesize') : 20;

        $where = array("level" => 1);
        $orderBy = array("id" => "asc");
        $categoryList = $this->TopicModel->getList($where, $page, $pagesize, $orderBy);
        $num = $this->TopicModel->getListNum($where);

        $url = 'index.php?c=topic&m=category_list';
        $multipage = multipage($num, $page, $pagesize, $url);

        $this->load->view('topic/category_list',array(
            "pageTitle" => $pageTitle,
            "categoryList" => $categoryList,
            "multipage" => $multipage,
            "page" => $page,
        ));
    }
    /**
     * 编辑标签
     */
    public function topic_edit(){
        $pageTitle = '编辑';
        $topic_id = $this->input->get_post('topic_id',true);
        $type = $this->input->get_post('type',true) ? $this->input->get_post('type',true) : 1; // 1:标签分类 2:标签列表
        $page = $this->input->get_post('page') ? $this->input->get_post('page') : 1;
        if($_REQUEST["submits"]){
            $param = $this->input->get_post('topic',true);
            $param["level"] = $type;
            if(empty($topic_id)){
                $this->TopicModel->insert_one($param);
                $this->AdminOpenationModel->insertLog($type,16,'添加话题：'.json_encode($param));
            }else{
                $this->TopicModel->update_data($param, array("id" => $topic_id));
                $this->AdminOpenationModel->insertLog($type,17,'修改话题：'.json_encode($param));
            }
            if($type == 1){
                showMessage("index.php?c=topic&m=category_list&page=".$page);
            }else{
                showMessage("index.php?c=topic&m=topic_list&page=".$page);
            }
        }
        $topic = array();
        if(!empty($topic_id)){
            $topic = $this->TopicModel->query_one(array("id"=>$topic_id));
        }
        $categoryList = array();
        if($type == 2){
            $where = array("status" => 1, "level" => 1);
            $orderBy = array("id" => "asc");
            $categoryList = $this->TopicModel->getList($where, 0, 0, $orderBy);
        }
        $this->load->view('topic/edit_topic',array(
            'pageTitle'=>$pageTitle,
            "topic" => $topic,
            "topic_id" => $topic_id,
            "type" => $type,
            "categoryList" => $categoryList,
            "page" => $page
        ));
    }

    /**
     * 设置标签的状态
     */
    public function set_status(){
        $id = $this->input->get_post('id',true);
        $status = $this->input->get_post('status',true);
        if(empty($id) || empty($status)){
            std_out(-1, "非法请求", "");
        }
        $id_arr=explode(',',$id);
        $where=array("id" => $id);
        if(!empty($id_arr)){
            $where="id in(".$id.")";
        }
        $param = array("status" => $status);
        $this->TopicModel->update_data($param, $where);
        $this->AdminOpenationModel->insertLog($id,18,'设置话题标签的状态：'.$id);

        std_out(0, "");
    }
    /**
     * 话题列表
     */
    public function topic_list(){
        $pageTitle = "话题列表";
        $page = $this->input->get_post('page') ? $this->input->get_post('page') : 1;
        $pagesize = $this->input->get_post('pagesize') ? $this->input->get_post('pagesize') : 20;
        $category_id = $this->input->get_post('category_id') ? $this->input->get_post('category_id') : '';
        $is_hot = $this->input->get_post('is_hot') ? $this->input->get_post('is_hot') : 'all';
        $s_title = $this->input->get_post('s_title') ? $this->input->get_post('s_title') : '';
        $s_num = $this->input->get_post('s_num') ? $this->input->get_post('s_num') : '';
        $is_hide = $this->input->get_post('is_hide') ? $this->input->get_post('is_hide') : '';
        $order = $this->input->get_post('order') ? $this->input->get_post('order') : 1;
        $category_id=intval($category_id);
        $where = array("level" => 2);
        $where['category_id']=$category_id;
        $where['s_num']=$s_num;
        $where['s_title']=$s_title;
        $where['is_hide']=$is_hide;
        if($is_hot != "all"){
            $where['is_hot'] = $is_hot;
        }
        if($order == 1){
            $orderBy = array("id","asc");
        }else{
            $orderBy = array("display_order","asc");
        }
        $topicList = $this->TopicModel->get_list($where, $page, $pagesize, $orderBy);
        $num = $this->TopicModel->get_list_num($where);

        $url = 'index.php?c=topic&m=topic_list&is_hide='.$is_hide.'&category_id='.$category_id.'&s_num='.$s_num.'&s_title='.$s_title."&is_hot=".$is_hot."&order=".$order;
        $multipage = multipage($num, $page, $pagesize, $url);

        $category_list=$this->TopicModel->getTopicOne();
        $this->load->view('topic/topic_list',array(
            "pageTitle" => $pageTitle,
            "topicList" => $topicList,
            "multipage" => $multipage,
            "page" => $page,
            "category_list" => $category_list,
            "is_hide" => $is_hide,
            "category_id" => $category_id,
            "s_num" => $s_num,
            "s_title" => $s_title,
        ));
    }
    /**
     * ajax请求查询列表
     * name: get_all_topic
     * @throws Exception
     * @Author:taiyang
     */
    public function get_all_topic(){
        $name = $this->input->get_post('topic_name') ? $this->input->get_post('topic_name') : '';
        $parent_id = $this->input->get_post('s_topic_id') ? $this->input->get_post('s_topic_id') : '';
        $list=$this->TopicModel->getTopicList($name,$parent_id);
        std_out(0,'',$list);
    }


    /**
     * 话题聚合页面
     * name: recommend_topic_list
     * @Author:taiyang
     */
    public function recommend_topic_list(){
        $pageTitle = "话题聚合页管理";
        $page = $this->input->get_post('page') ? $this->input->get_post('page') : 1;
        $pagesize = $this->input->get_post('pagesize') ? $this->input->get_post('pagesize') : 10;
        //获取推荐列表
        $where=array();
        $list=$this->TopicRecommendModel->getList($where,$page,$pagesize,array('is_show'=>'desc','sort'=>'desc'));
        $new_list=array();
        foreach ($list as $k=>$v){
            $new_list[$k]=$this->TopicRecommendModel->get_details($v);
        }

        $num = $this->TopicRecommendModel->getListNum($where);

        $url = 'index.php?c=topic&m=recommend_topic_list';
        $multipage = multipage($num, $page, $pagesize, $url);

        $this->load->view('topic/t_topic_list',array(
            "pageTitle" => $pageTitle,
            "list"=>$new_list,
            "multipage" => $multipage,
            "page" => $page,
        ));
    }

    /**
     * 默认规则帖子列表
     * name: recommend
     * @throws Exception
     * @Author:taiyang
     */
    public function recommend(){
        $pageTitle = "话题规则";
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        $where_res=$this->TopicRecommendModel->get_where($id);
        $list=$this->PostModel->getPostAll($where_res['where'],1,10,$where_res['order']);
        $this->load->view('topic/recommend',array(
            "pageTitle" => $pageTitle,
            "list"=>$list,
            "id"=>$id
        ));
    }

    /**
     * 定制的数据列表
     * name: custom_list
     * @throws Exception
     * @Author:taiyang
     */
    public function custom_list(){
        $pageTitle = "话题规则";
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        $topic_res=$this->TopicRecommendModel->query_one(array('id'=>$id));
        $topic=$this->TopicRecommendModel->get_details($topic_res);
        $specify_id=$topic['specify'];
        if(!empty($specify_id)){
            $id_in=array();
            foreach ($specify_id as $k => $v){
                $id_in[$v['sort']]=$v['id'];
            }
            ksort($id_in);
            $where['s_type_id']=1;
            $where['s_content']=implode(',',$id_in);
            $list=$this->PostModel->getPostAll($where,1,10,9);
        }else{
            $list=array();
        }
        $res=$id_in;
        foreach ($list as $key=>$vel){
            foreach ($specify_id as $ik => $iv){
                if($vel['id'] == $iv['id']){
                    $res[$iv['sort']]=$vel;
                }
            }
        }
        $this->load->view('topic/custom_list',array(
            "pageTitle" => $pageTitle,
            "list"=>$res,
            "id"=>$id
        ));
    }
    /**
     * 定制的数据列表
     * name: custom_list
     * @throws Exception
     * @Author:taiyang
     */
    public function del_list(){
        $pageTitle = "话题规则";
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        $topic_res=$this->TopicRecommendModel->query_one(array('id'=>$id));
        $topic=$this->TopicRecommendModel->get_details($topic_res);
        $remove_id=$topic['remove_id'];
        $list=array();
        if(!empty($remove_id)){
            $arr=explode(',',$remove_id);
            $arr=array_filter(array_flip(array_flip($arr)));//去重，去空
            $data=implode(',',$arr);
            $where['s_type_id']=1;
            $where['s_content']=$data;
            $list=$this->PostModel->getPostAll($where,1,10,9);

        }
        $this->load->view('topic/custom_list',array(
            "pageTitle" => $pageTitle,
            "list"=>$list,
            "id"=>$id
        ));
    }

    /**
     * 推荐最终列表
     * name: result_list
     * @throws Exception
     * @Author:taiyang
     */
    public function result_list(){
        $pageTitle = "话题规则";
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        $topic_res=$this->TopicRecommendModel->query_one(array('id'=>$id));
        $topic=$this->TopicRecommendModel->get_details($topic_res);
        $specify_id=$topic['specify'];
        $id_in=array();
        if(!empty($specify_id)){
            foreach ($specify_id as $k => $v){
                $id_in[$v['sort']]=$v['id'];
            }
            ksort($id_in);
            $where['s_type_id']=1;
            $where['s_content']=implode(',',$id_in);
            //定制数据
            $list=$this->PostModel->getPostAll($where,1,10,9);
        }else{
            $list=array();
        }
        $res=$id_in;
        foreach ($list as $key=>$vel){
            foreach ($specify_id as $ik => $iv){
                if($vel['id'] == $iv['id']){
                    $res[$iv['sort']]=$vel;
                }
            }
        }
        $num=count($id_in);
        //正常数据
        $where_res=$this->TopicRecommendModel->get_where($id);
        $post_list=$this->PostModel->getPostAll($where_res['where'],1,10-$num,$where_res['order']);
        //全部两个帖子数据
        foreach ($res as $sk => $sv){
            array_splice($post_list,$sk-1,0,array($sv));
        }
        $this->load->view('topic/custom_list',array(
            "pageTitle" => $pageTitle,
            "list"=>$post_list,
            "id"=>$id
        ));
    }

    /**
     * 添加指定帖子
     * name: add_custom
     * @throws Exception
     * @Author:taiyang
     */
    public function add_custom(){
        $pageTitle = "话题规则";
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        $topic_res=$this->TopicRecommendModel->query_one(array('id'=>$id));
        $topic=$this->TopicRecommendModel->get_details($topic_res);
        if($_REQUEST["submits"]) {
            $post_id = $this->input->get_post('post_id') ? $this->input->get_post('post_id') : '';
            $sort = $this->input->get_post('sort') ? $this->input->get_post('sort') : '';
            $data=array('id'=>$post_id,'sort'=>$sort);
            if(!empty($topic['specify'])){
                if(in_array($data,$topic['specify'])){
                    showMessage("index.php?c=topic&m=custom_list&id=".$id,1,'数据错误');
                }
                $data_merge=array_merge(array($data),$topic['specify']);
            }else{
                $data_merge=array($data);
            }
            $this->TopicRecommendModel->update_data(array('specify_id'=>json_encode($data_merge)),array('id'=>$id));
            showMessage("index.php?c=topic&m=custom_list&id=".$id);
        }
        $id_arr=array();$sort_arr=array();
        if($topic['specify']){
            foreach ($topic['specify'] as $k=>$v){
                $id_arr[]=$v['id'];
                $sort_arr[]=$v['sort'];
            }
        }
        $sort_arr=array_filter(array_flip(array_flip($sort_arr)));
        $id_arr=array_filter(array_flip(array_flip($id_arr)));
        sort($sort_arr);
        sort($id_arr);
        array_merge($id_arr);
        $this->load->view('topic/add_custom',array(
            "pageTitle" => $pageTitle,
            "id_arr"=>implode(',',$id_arr),
            "sort_arr_json"=>implode(',',array_merge($sort_arr)),
            "sort_arr"=>$sort_arr,
            "id"=>$id,
            "topic"=>$topic
        ));

    }


    /**
     * 移除指定帖子
     * name: add_custom
     * @throws Exception
     * @Author:taiyang
     */
    public function del_custom(){
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        $topic_res=$this->TopicRecommendModel->query_one(array('id'=>$id));
        $topic=$this->TopicRecommendModel->get_details($topic_res);
        $post_id = $this->input->get_post('post_id') ? $this->input->get_post('post_id') : '';
        if(!empty($topic['specify'])){
            $data=array();
            foreach ($topic['specify'] as $k => $v){
                if($v['id']!==$post_id){
                    $data[]=$v;
                }
            }
            $this->TopicRecommendModel->update_data(array('specify_id'=>json_encode($data)),array('id'=>$id));
            echo json_encode(array('errorCode' => 0, 'errorMsg' => '成功'));
        }else{
            //删除错误
            echo json_encode(array('errorCode' => -1, 'errorMsg' => '参数错误'));
        }
    }


    /**
     * 编辑规则
     * name: edit_rule
     * @throws Exception
     * @Author:taiyang
     */
    public function edit_rule(){
        $pageTitle = "话题规则";
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        $topic_res=$this->TopicRecommendModel->query_one(array('id'=>$id));
        $topic=$this->TopicRecommendModel->get_details($topic_res);
        if($_REQUEST["submits"]) {
            $rule_json=json_encode($_POST);
            $this->TopicRecommendModel->update_data(array('rule'=>$rule_json),array('id'=>$id));
            showMessage("index.php?c=topic&m=recommend&id=".$id);
        }
        $this->load->view('topic/edit_rule',array(
            "pageTitle" => $pageTitle,
            "id"=>$id,
            "topic"=>$topic
        ));

    }

    /**
     * 从推荐中移除帖子
     * name: del_post
     * @throws Exception
     * @Author:taiyang
     */
    public function del_post(){
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        $post_id = $this->input->get_post('post_id') ? $this->input->get_post('post_id') : '';
        if (empty($id)||empty($post_id)) {
            echo json_encode(array('errorCode' => -1, 'errorMsg' => '参数错误'));
        }
        $topic = $this->TopicRecommendModel->query_one(array('id' => $id));
        $remove_id=$topic['remove_id'];
        if(empty($remove_id)){
            $data=$post_id;
        }else{
            $arr=explode(',',$remove_id.','.$post_id);
            $arr=array_filter(array_flip(array_flip($arr)));//去重，去空
            $data=implode(',',$arr);
        }
        $res = $this->TopicRecommendModel->update_data(array('remove_id' => $data), array('id' => $id));
        if ($res != false) {
            echo json_encode(array('errorCode' => 0, 'errorMsg' => '成功'));
        } else {
            echo json_encode(array('errorCode' => -1, 'errorMsg' => '参数错误'));
        }

    }

    /**
     * 从移除列表中还原帖子
     * name: update_post
     * @throws Exception
     * @Author:taiyang
     */
    public function update_post(){
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        $post_id = $this->input->get_post('post_id') ? $this->input->get_post('post_id') : '';
        if (empty($id)||empty($post_id)) {
            echo json_encode(array('errorCode' => -1, 'errorMsg' => '参数错误'));
        }
        $topic = $this->TopicRecommendModel->query_one(array('id' => $id));
        $remove_id=$topic['remove_id'];
        if(empty($remove_id)){
            echo json_encode(array('errorCode' => 0, 'errorMsg' => '数据不存在！'));
        }else{
            $arr=explode(',',$remove_id);
            $arr=array_flip($arr);
            unset($arr[$post_id]);
            $arr=array_filter(array_flip($arr));
            $data=implode(',',$arr);
        }
        $res = $this->TopicRecommendModel->update_data(array('remove_id' => $data), array('id' => $id));
        if ($res != false) {
            echo json_encode(array('errorCode' => 0, 'errorMsg' => '成功'));
        } else {
            echo json_encode(array('errorCode' => -1, 'errorMsg' => '参数错误'));
        }

    }

    /**
     * 新增话题推荐
     * name: add_recommend
     * @throws Exception
     * @Author:taiyang
     */
    public function add_recommend(){
        $pageTitle = "话题推荐";
        if($_REQUEST["submits"]) {
            $topic_id=$this->input->get_post('s_topic_id')?$this->input->get_post('s_topic_id'):0;
            $sort=$this->input->get_post('sort')?$this->input->get_post('sort'):0;
            $this->TopicRecommendModel->insert_one(array('topic_id'=>$topic_id,'sort'=>$sort,'is_show'=>-1,'add_time'=>time()));
            showMessage("index.php?c=topic&m=recommend_topic_list");
        }
        $this->load->view('topic/add_recommend',array(
            "pageTitle" => $pageTitle,
        ));
    }

    /**
     * 推荐话题是否显示在前端
     * name: up_is_show
     * @throws Exception
     * @Author:taiyang
     */
    public function up_is_show()
    {
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        if (empty($id)) {
            echo json_encode(array('errorCode' => -1, 'errorMsg' => '参数错误'));
        }
        $topic = $this->TopicRecommendModel->query_one(array('id' => $id));
        $is_show = $topic['is_show'] == 1 ? -1 : 1;
        $res = $this->TopicRecommendModel->update_data(array('is_show' => $is_show), array('id' => $id));
        if ($res != false) {
            echo json_encode(array('errorCode' => 0, 'errorMsg' => '成功'));
        } else {
            echo json_encode(array('errorCode' => -1, 'errorMsg' => '参数错误'));
        }
    }
    /**
     * 删除推荐话题
     * name: up_is_show
     * @throws Exception
     * @Author:taiyang
     */
    public function del_recommend()
    {
        $id = $this->input->get_post('id') ? $this->input->get_post('id') : '';
        if (empty($id)) {
            echo json_encode(array('errorCode' => -1, 'errorMsg' => '参数错误!'));
        }
        $res = $this->TopicRecommendModel->del_data(array('id'=>$id));
        if ($res != false) {
            echo json_encode(array('errorCode' => 0, 'errorMsg' => '成功'));
        } else {
            echo json_encode(array('errorCode' => -1, 'errorMsg' => '参数错误!!'));
        }
    }
    /**
     * 添加 / 取消 热门话题
     */
    public function set_hot(){
        $topic_id = $this->input->get_post('topic_id');
        $status = $this->input->get_post('status') ? $this->input->get_post('status') : 0;
        if(empty($topic_id)){
            std_out(-1, "话题为空", "");
        }
        $topicIds = explode(",", $topic_id);
        $param = array("is_hot" => $status);
        foreach($topicIds as $t_id){
            $this->TopicModel->update_data($param, array("id" => $t_id));
        }
        std_out(0, "");
    }
    /**
     * 修改话题聚合页面排序
     * name: up_sort
     * @throws Exception
     * @Author:taiyang
     */
    public function up_sort(){
        $id=$this->input->get_post('id')?$this->input->get_post('id'):'';
        $sort=$this->input->get_post('sort')?$this->input->get_post('sort'):'';
        if(empty($id) || empty($sort)){
            echo json_encode(array('status'=>-1,'msg'=>'排序不能为空……'));exit;
        }
        $this->TopicRecommendModel->update_data(array('sort'=>$sort),array('id'=>$id));
        //添加修改人信息
        $this->AdminOpenationModel->insertLog($id,26,'修改话题聚合页面排序，话题id:'.$id);
        echo json_encode(array('status'=>1,'msg'=>'修改成功！'));exit;
    }

}